using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Identity.Application.Contracts.Requests.Permission;
using Identity.Application.Permissions.Commands;
using Identity.Application.Permissions.Queries;
using Identity.HttpAPI.Permission;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Identity.HttpAPI.Controllers
{
    [Authorize]
    [ApiController]
    [Route("api/permissions")]
    public class PermissionController : ControllerBase
    {
        private readonly IMediator _mediator;
        public PermissionController(IMediator mediator)
        {
            _mediator = mediator;
        }


        [HttpGet]
        [Authorize(Roles = "Admin")]
        [Permission("permissions:items:get", "获取全部权限")]
        public async Task<IActionResult> GetPermissions()
        {
            var query = new GetAllPermissionQuery();
            var result = await _mediator.Send(query);
            return Ok(result);
        }
        [HttpGet("unboundApi")]
        [Authorize(Roles = "Admin")]
        [Permission("permissions:items:unbound:get", "获取未绑定接口权限")]
        public async Task<IActionResult> GetUnboundPermissions()
        {
            var query = new GetUnboundPermissionsForApiQuery();
            var result = await _mediator.Send(query);
            return Ok(result);
        }

         [HttpGet("boundApi")]
        [Authorize(Roles = "Admin")]
        [Permission("permissions:items:bound:view", "获取未绑定接口权限")]
        public async Task<IActionResult> GetBoundPermissions()
        {
            var query = new GetBoundPermissionsForApiQuery();
            var result = await _mediator.Send(query);
            return Ok(result);
        }



        [HttpGet("{permissionId}")]
        [Authorize(Roles = "Admin")]
        [Permission("permissions:item:get", "获取权限")]
        public async Task<IActionResult> GetPermissionById(Guid permissionId)
        {
            var query = new GetPermissionByIdQuery(permissionId);
            var result = await _mediator.Send(query);
            return Ok(result);
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        [Permission("permissions:item:create", "创建权限")]
        public async Task<IActionResult> CreatePermission([FromBody] CreatePermissionRequest rq)
        {
            var command = new CreatePermissionCommand(rq.key, rq.name, rq.description);
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpDelete("{permissionId}")]
        [Authorize(Roles = "Admin")]
        [Permission("permissions:item:delete", "删除权限")]
        public async Task<IActionResult> DeletePermission(Guid permissionId)
        {
            var command = new DeletePermissionCommand(permissionId);
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpPut("{permissionId}")]
        [Authorize(Roles = "Admin")]
        [Permission("permissions:item:update", "更新权限")]
        public async Task<IActionResult> UpdatePermission(Guid permissionId, [FromBody] UpdatePermissionRequest rq)
        {
            var command = new UpdatePermissionCommand(permissionId, rq.key,rq.name, rq.description);
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpPatch("nameChange")]
        [Authorize(Roles = "Admin")]
        [Permission("permissions:item:name:change", "修改权限名称")]
        public async Task<IActionResult> ChangePermissionName([FromBody] ChangePermissionNameRequest rq)
        {
            var command = new ChangePermissionNameCommand(rq.Id, rq.name);
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpPatch("descriptionChange")]
        [Authorize(Roles = "Admin")]
        [Permission("permissions:item:description:change", "修改权限描述")]
        public async Task<IActionResult> ChangePermissionDescription([FromBody] ChangePermissionDescriptionRequest rq)
        {
            var command = new ChangePermissionDescriptionCommand(rq.Id, rq.description);
            var result = await _mediator.Send(command);
            return Ok(result);
        }

    }
}